package com.bestcem.xm.user.dao.impl;

import com.alibaba.excel.util.StringUtils;
import com.bestcem.xm.user.dao.UserUserGroupDao;
import com.bestcem.xm.user.dao.mapper.UserUserGroupMapper;
import com.bestcem.xm.user.entity.mysql.UserUserGroup;
import com.bestcem.xm.user.entity.pojo.UserUserGroupDO;
import com.bestcem.xm.user.entity.pojo.UserWithGroupDO;
import com.bestcem.xm.user.grpc.service.param.GroupIdsPageParam;
import com.bestcem.xm.user.util.business.DataBaseUtil;
import com.bestcem.xm.user.util.convert.UserUserGroupConvert;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户与用户组的关联信息daoImpl
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2021/12/29 10:38
 */
@Repository
public class UserUserGroupDaoImpl implements UserUserGroupDao {

    @Resource
    private UserUserGroupMapper userUserGroupMapper;

    @Resource
    private UserUserGroupConvert userUserGroupConvert;

    @Override
    public String saveUserToGroup(UserUserGroupDO userUserGroupDO) {
        DataBaseUtil.setCreateAndUpdateTime(userUserGroupDO);
        if (StringUtils.isBlank(userUserGroupDO.getUserGroupId())) {
            userUserGroupDO.setUserGroupId(DataBaseUtil.generateId());
        }
        userUserGroupMapper.saveUserToGroup(userUserGroupDO);
        return userUserGroupDO.getUserGroupId();
    }

    @Override
    public Integer insertUserGroupList(List<UserUserGroupDO> dos) {
        List<UserUserGroup> userUserGroups = userUserGroupConvert.dosToEntitys(dos);
        for (UserUserGroup userUserGroup : userUserGroups) {
            DataBaseUtil.setCreateAndUpdateTime(userUserGroup);
        }
        return userUserGroupMapper.insertUserGroupList(userUserGroups);
    }

    @Override
    public Integer deleteBySelective(UserUserGroupDO userUserGroupDO) {
        UserUserGroup userUserGroup = userUserGroupConvert.doToEntity(userUserGroupDO);
        return userUserGroupMapper.deleteBySelective(userUserGroup, new ArrayList<>());
    }

    @Override
    public List<String> getUserIdsByGroupId(String groupId) {
        return userUserGroupMapper.getUserIdsByGroupId(groupId);
    }

    @Override
    public List<String> getUserIdsByGroupIds(GroupIdsPageParam param) {
        if(Objects.isNull(param.getPageParam())){
            return userUserGroupMapper.getUserIdsByGroupIds(0, 0, param.getGroupIds());
        }
        return userUserGroupMapper.getUserIdsByGroupIds(param.getPageParam().getSize(), param.getPageParam().getOffset(), param.getGroupIds());
    }

    @Override
    public Integer getUserCountByGroupIds(List<String> groupIds) {
        return userUserGroupMapper.getUserCountByGroupIds(groupIds);
    }

    @Override
    public List<String> listUserIds(String groupId, List<String> userIds) {
        return userUserGroupMapper.selectUserIdsByGroupIdAndUserIds(groupId, userIds);
    }

    @Override
    public Integer deleteByGroupIdAndUserIds(String groupId, List<String> userIds) {
        UserUserGroupDO param = new UserUserGroupDO();
        param.setGroupId(groupId);
        return userUserGroupMapper.deleteBySelective(param, userIds);
    }

    @Override
    public List<UserUserGroupDO> listByCondition(UserUserGroupDO condition) {
        // 设置查询参数
        UserUserGroup param = userUserGroupConvert.doToEntity(condition);
        // 查询
        List<UserUserGroup> results = userUserGroupMapper.selectBySelective(param);
        // 设置返回的查询结果
        return userUserGroupConvert.entitysToDos(results);
    }

    @Override
    public List<UserUserGroupDO> listByUserIdAndGroupIds(String userId, List<String> groupIdsList) {
        List<UserUserGroup> userUserGroups = userUserGroupMapper.listByUserIdAndGroupIds(userId, groupIdsList);
        return userUserGroupConvert.entitysToDos(userUserGroups);
    }

    @Override
    public int updateOperateTime(String userId, List<String> alreadyGroupIds) {
        return userUserGroupMapper.updateOperateTime(userId, alreadyGroupIds, DataBaseUtil.getDate());
    }

    @Override
    public List<UserUserGroupDO> getUserUserGroupByEachId(UserUserGroupDO userUserGroupDO, Integer page, Integer size) {
        List<UserUserGroup> userUserGroups = userUserGroupMapper.getByEachId(userUserGroupDO, (page - 1) * size, size);
        return userUserGroupConvert.entitysToDos(userUserGroups);
    }

    @Override
    public List<String> getGroupIdsByUserId(String userId) {
        return userUserGroupMapper.getGroupIds(userId);
    }

    @Override
    public Map<String, Integer> countUserNumByGroupIds(List<String> groupIds) {
        Map<String, Integer> map = new HashMap<>(16);
        List<UserUserGroup> userUserGroups = userUserGroupMapper.countUserNumByGroupIds(groupIds);
        if (CollectionUtils.isNotEmpty(userUserGroups)) {
            for (UserUserGroup userUserGroup : userUserGroups) {
                map.put(userUserGroup.getGroupId(), userUserGroup.getUserNum());
            }
        }
        return map;
    }

    @Override
    public List<UserWithGroupDO> getUserWithGroup(String userId) {
        return userUserGroupMapper.getUserWithGroup(userId);
    }

    @Override
    public Long count(UserUserGroupDO userUserGroupDO) {
        return userUserGroupMapper.count(userUserGroupDO);
    }

}
